<template>
  <div class="register-page">
    <van-nav-bar title="注册页面" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' },{ pattern: /^[a-zA-Z0-9]{6,10}$/, message: '请输入6-10位数字或字母的用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' },{ pattern: /^[0-9]{6,10}$/, message: '请输入6-10位全部为数字的密码' }]"
      />
      <div style="margin: 16px;">
        <van-button block type="primary" native-type="submit">注册</van-button>
      </div>
      <router-link class="log" to="/login">去登陆</router-link>
    </van-form>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'register-page',
  props: {},
  components: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  computed: {

  },
  watch: {},
  methods: {
    async onSubmit () {
      try {
        await register({
          username: this.username,
          password: this.password
        })
        this.$router.push('/login')
      } catch (error) {
        alert(error)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.register-page {
  height: 100vh;
  background-color: #fff;
}
.log{
  display: flex;
  justify-content: flex-end;
  padding-right: 20px;
  color: #069;
  font-size: 12px;
}
</style>
